#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
# @Time      :2020/4/19
# @Author    :zhangxuchuo
# @Email     :zhangxuchuo@bbdservice.com
# @File      :pricing_amount_ICindex_2D
# @Software  :PyCharm

'''eof
name:企业主贷款余额
code:pricing_amount_ICindex_2D
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:
eof'''


import json
import sys
import re
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or len(table) == 0 or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]

def nullCheck(str):
    if str not in null_type_list:
        return True
    else:
        return False

def pricing_amount_ICindex_2D():
    factor = []
    data = IC_DSJ_2D.get('data')
    # 借贷账户信息 PDA [1..1]
    PDA = getTable(data, 'PDA', 1, 0)
    if PDA == [{}]:
        return 0
    elif PDA == 0:
        return 0
    else:
        # 借贷账户信息单元 PD01 [0..*]
        PD01 = getTable(PDA[0], 'PD01', 0, 1)
        if PD01 == [{}]:
            return 0
        elif PD01 == 0:
            return 0
        else:
            for i in range(len(PD01)):
               # 基本信息段 PD01A [1..1]
               # 最新表现信息段 PD01B [1..1]
                PD01A = getTable(PD01[i], 'PD01A', 1, 1)
                PD01B = getTable(PD01[i], 'PD01B', 1, 1)
                if PD01A == [{}] or PD01B == [{}]:
                    factor.append(0)
                elif PD01A == 0 or PD01B == 0:
                    factor.append(0)
                else:
                    # 业务种类 PD01AD03 [0..1]
                    PD01AD03 = PD01A[0].get('PD01AD03')
                    # 余额 PD01BJ01 [1..1]
                    PD01BJ01 = PD01B[0].get('PD01BJ01')
                    if nullCheck(PD01AD03) and nullCheck(PD01BJ01):
                        try:
                            if PD01AD03 == '52' or PD01AD03 == '41':
                                factor.append(float(PD01BJ01))
                        except:
                            pass
    factor = sum(factor)
    return factor




if __name__ == '__main__':
    file_obj = open(r'IC_DSJ_2D.json', 'r')
    content = file_obj.read()
    IC_DSJ_2D = json.loads(content, strict=False)
    res = pricing_amount_ICindex_2D()
    print(res)

result = pricing_amount_ICindex_2D()